package com.wangsd.web.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * 网络请求工具类
 */
public class HttpUtils {

    /**
     * 发起GET请求
     * @param url 请求连接
     * @return 请求结果，Json字符串
     */
    public static String doGet(String url) {
        HttpGet httpGet = new HttpGet(url);
        String result = null;
        try(CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(httpGet)) {
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}